package com.hhsjtest.bus_enter.caller

import android.content.res.AssetManager
import android.os.Handler
import android.os.Looper
import android.os.Message
import com.hhsjdesign.dynamicui.annoprocess.ABusinessCaller
import com.hhsjtest.bus_enter.businessdata.Enter
import com.hhsjdesign.dynamicui.interfaces.RequestListener
import com.hhsjdesign.dynamicui.businessdata.BaseModel
import com.hhsjdesign.dynamicui.utils.DataCaller
import com.hhsjdesign.dynamicui.core.widgetimpl.IWidget
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
import kotlin.Exception

@ABusinessCaller("enter")
class EnterDataCaller(businessType: String) : DataCaller<Enter>(businessType = "enter") {

    override fun loadData(
        businessType: String,
        businessParams: String,
        widget: IWidget,
        listener: RequestListener<Any>
    ) {
        val handler = object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                val enter: BaseModel<Any> = msg.obj as BaseModel<Any>
                if (enter.code == 0) {
                    listener.onRequestSuccess(businessType, enter)
                } else {
                    listener.onRequestFail(businessType, enter.code, enter.message)
                }
            }
        }

        //协程处理
        GlobalScope.launch {
            try {
                val assetManager: AssetManager = widget.context.assets!!
                val inputStream = assetManager.open(businessParams)
                val stringBuilder = StringBuilder()
                val reader = BufferedReader(InputStreamReader(inputStream))
                var line: String? = null
                while ((reader.readLine().also { line = it }) != null) {
                    stringBuilder.append(line)
                }
                reader.close()
                inputStream.close()
                val jo = JSONObject(stringBuilder.toString())

                val enter = BaseModel<Enter>()
                enter.code = jo.optInt("code", 0)
                enter.message = jo.optString("message", "success")
                enter.type = 1
                enter.resultList = ArrayList()
                val array = jo.optJSONObject("data")?.optJSONArray("result")
                array?.apply {
                    for (i in 0 until this.length()) {
                        Enter.fromJSON(
                            widget.widgetData.widgetId,
                            widget.widgetData.widgetTreeId,
                            this.optJSONObject(i)
                        )?.let {
                            enter.resultList!!.add(it)
                        }
                    }
                }
                val message = Message.obtain()
                message.obj = enter
                handler.sendMessageDelayed(message, 0)
            } catch (e: Exception) {
                val enter = BaseModel<Enter>()
                enter.code = -1
                enter.message = "失败"
                val message = Message.obtain()
                message.obj = enter
                handler.sendMessageDelayed(message, 0)
            }
        }
    }

    override fun cancelLoad() {
        super.cancelLoad()
    }

}